#include <iostream>
#include <vector>
#include <string>
#include <chrono>

// compile command: g++ -std=c++14 emplace_stringv2.cpp

int main() {


    std::vector<std::string> v;
    int count = 10000000;
    v.reserve(count);       //预分配十万大小，排除掉分配内存的时间

    auto get_milisec = [](auto& start){
        std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start).count() << "ms\n";
    };

    {
        std::cout << "v.push_back(obj): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            std::string temp("ceshi");
            v.push_back(temp);// push_back(const string&)，参数是左值引用
        }
        get_milisec(start);
    }

    {
        std::cout << "v.emplace_back(obj): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            std::string temp("ceshi");
            v.emplace_back(temp);// push_back(const string&)，参数是左值引用
        }
        get_milisec(start);
    }

    v.clear();
    {
        std::cout << "v.push_back(std::move(obj)): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            std::string temp("ceshi");
            v.push_back(std::move(temp));// push_back(string &&), 参数是右值引用
        }
        get_milisec(start);
    }

    v.clear();
    {
        std::cout << "v.emplace_back(std::move(obj)): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            std::string temp("ceshi");
            v.emplace_back(std::move(temp));// push_back(string &&), 参数是右值引用
        }
        get_milisec(start);
    }

    v.clear();
    {
        std::cout << "v.push_back(std::string(string)): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            v.push_back(std::string("ceshi"));// push_back(string &&), 参数是右值引用
        }
        get_milisec(start);
    }

    v.clear();
    {
        std::cout << "v.emplace_back(std::string(string)): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            v.emplace_back(std::string("ceshi"));// push_back(string &&), 参数是右值引用
        }
        get_milisec(start);
    }

    v.clear();
    {
        std::cout << "v.emplace_back(std::move(std::string(string))): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            v.emplace_back(std::move(std::string("ceshi")));// push_back(string &&), 参数是右值引用
        }
        get_milisec(start);
    }

    v.clear();
    {
        std::cout << "v.push_back(c_string)): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            v.push_back("ceshi");// push_back(string &&), 参数是右值引用
        }
        get_milisec(start);
    }

    v.clear();
    {
        std::cout << "v.emplace_back(c_string)): ";
        auto start = std::chrono::system_clock::now();
        for (int i = 0; i < count; i++)
        {
            v.emplace_back("ceshi");// 只有一次构造函数，不调用拷贝构造函数，速度最快
        }
        get_milisec(start);
    }
}